package com.example.demo.core.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

@Component
public class MybatisPlusFieldFillHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject meta) {
        this.fillStrategy(meta, "createTime", System.currentTimeMillis());
        this.fillStrategy(meta, "updateTime", System.currentTimeMillis());
        this.fillStrategy(meta, "deleted", 0);
    }

    @Override
    public void updateFill(MetaObject meta) {
        this.fillStrategy(meta, "updateTime", System.currentTimeMillis());
    }

    @Override
    public MetaObjectHandler fillStrategy(MetaObject metaObject, String fieldName, Object fieldVal) {
        setFieldValByName(fieldName, fieldVal, metaObject);
        return this;
    }
}
